"""
请求百度翻译
post 请求的参数必须要进行编码，转换为字节类型
"""

import urllib.request
import urllib.parse
import json

url = "https://fanyi.baidu.com/sug"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"
}
word = {
    "kw": "hello"
}

# post 请求的参数必须要进行编码，转换为字节类型
data = urllib.parse.urlencode(word).encode("utf-8")

req = urllib.request.Request(url=url, data=data, headers=headers)

response = urllib.request.urlopen(req)

content = response.read().decode("utf-8")
result = json.loads(content)
print(result)
